﻿using System.Threading;
using System.Threading.Tasks;
using Iot.RealTimeData.RealTimeData;
using Iot.Shared.Model;
using Mediator;
using Microsoft.Extensions.Logging;

namespace Iot.RealTimeData.RealTimeDataEvent;

public sealed class RealTimeDataEventInteger(
    IRealTimeData<long?, IOnIntegerValue> data,
    ILogger<RealTimeDataEventInteger> logger)
    : INotificationHandler<IntegerBusModel>
{
    // 实现INotificationHandler<AnalogFloatBusModel>接口的Handle方法
    public ValueTask Handle(IntegerBusModel notification, CancellationToken cancellationToken)
    {
        // 调用_data的SetValue方法，将notification.Path、notification.Value、notification.Timestamp作为参数传入
        logger.LogTrace("函数:{函数},开始处理通知{notification}.", nameof(Handle), notification);
        data.SetValue(notification.Path, notification.Value, notification.TimeStamp);
        logger.LogTrace("函数:{函数},通知{notification}设置值成功.", nameof(Handle), notification);
        // 返回一个Task.CompletedTask
        return ValueTask.CompletedTask;
    }
}